package com.example.zhangxin.broadnotifytest_124zx;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

/**
 * Created by zhangxin on 2017/5/3.
 */

public class MyReceiver extends BroadcastReceiver {
    private Context context;//存上下文信息
    private String str;//接收到广播发来的消息
    //通知的管理器
    public static NotificationManager manager;
    public static int notifyID=1;//通知的ID

    @Override
    public void onReceive(Context context, Intent intent) {//里面上下文，数据
        this.context=context;
        str=intent.getStringExtra("para2");//接收
        Toast.makeText(context, "接收到广播"+str, Toast.LENGTH_SHORT).show();

        //发送通知
        sendnotify();

    }

    private void sendnotify() {
        //1--获取通知管理器
        manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        //2--实例化通知构造器
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
        //3--对构造器进行配置
        //点击后进入哪个窗口activity
        Intent intent=new Intent(context,NotifyActivity.class);
        intent.putExtra("para3",str);//传参
        PendingIntent pintent=PendingIntent.getActivity(
          context,//上下文
                notifyID,//通知ID
                intent,//intent
                PendingIntent.FLAG_UPDATE_CURRENT//更新标志
        );


        //配置标题，内容，图标等信息

        builder.setContentTitle("通知的标题")
                .setContentText("通知的内容"+str)
                .setSmallIcon(R.mipmap.ic_launcher)//图标
                .setTicker("通知来了")
                .setContentIntent(pintent)//跳转到哪个
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)//点击内容通知自动取消
                .setWhen(System.currentTimeMillis());//通知时间
        //发送通知
        manager.notify(notifyID,builder.build());
    }
}
